package com.action
{
	import com.logic.BattleLogic;
	import com.resource.unit.ActionUnit;
	import com.resource.unit.BaseUnit;
	import com.stage.item.BasePlayer;
	import com.stage.item.Player;
	import com.utils.time.TimerClock;
	
	import flash.geom.Point;

	public class ActionAttack extends BaseAction
	{
		public var skillId:String;
		private var _threadId:int = 0;
		
		public function ActionAttack(player:Player, skillId:String)
		{
			super(player);
			this.skillId = skillId;
		}
		
		public override function start(dir:int=1):void{
			_dir = dir;
		//	_player.act = ActionUnit.ATTACK;
			_player.act = skillId;
			
			// 如果是主机，添加各种检查
			if(BattleLogic.hostId == BattleLogic.aid){
				_threadId = TimerClock.getInstance().addEnterFrame(enterFrame);
			}
			
			_player.addActionEndListener(actionEnd);
		}
		
		private function actionEnd(player:Player):void{
			_player.action = null; // 会调用end()
		}
		
		protected override function enterFrame():void{
			
			// 测试时，如果思attack
			var sendBullet:Object = _player.view.getSendBullet();
			if(sendBullet && sendBullet.x1 < sendBullet.x2){
				var position:Point = _player.view.getPosition();
				BattleLogic.sendBullet(_player.data.aid, skillId, _player.dir, _player.x + (sendBullet.x1 - position.x) * _player.dir, _player.y + (sendBullet.y1 - position.y));
			}
			
			// 是skill时
			BattleLogic.playerHitPlayer(this);
		}
		
		public override function end():void{
			_player.act = ActionUnit.STAND;
			_player.removeActionEndListener(actionEnd);
			if(_threadId > 0){
				TimerClock.getInstance().removeEnterFrame(_threadId);
				_threadId = 0;
			}
		}
	}
}